package com.leapcloud.crm.core.utils;

/**
 * 数据 转化 工具
 *
 * @author wangzhenyan
 * @email ffu@maxleap.com
 * @since 2018/9/7 14:12
 */
public class TransformUtils {

  public static long toLong(String value) {
    return toLong(value, 0L);
  }

  public static long toLong(String value, Long defaultValue) {
    try {
      return Long.valueOf(value);
    } catch (Exception e) {
      return defaultValue;
    }
  }

  public static long toLong(Double value) {
    if (value == null) {
      return 0;
    }
    return value.longValue();
  }


  public static int toInt(String value) {
    return toInt(value, 0);
  }

  public static int toInt(Float value) {
    if (value == null) {
      return 0;
    }
    return value.intValue();
  }

  public static float toFloat(Double value) {
    if (value == null) {
      return 0;
    }
    return value.floatValue();
  }

  public static int toInt(String value, Integer defaultValue) {
    try {
      return Integer.valueOf(value);
    } catch (Exception e) {
      return defaultValue;
    }
  }

  public static double toDouble(String value) {
    return toDouble(value, 0d);
  }

  public static double toDouble(String value, Double defaultValue) {
    try {
      return Double.valueOf(value);
    } catch (Exception e) {
      return defaultValue;
    }
  }

  public static String toLowerCaseFirstOne(String s) {
    if (Character.isLowerCase(s.charAt(0))) {
      return s;
    } else {
      return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
    }
  }

  public static String cutString(Object obj, int maxLength) {
    String result = "";
    if (obj != null) {
      if (obj.toString().length() < maxLength) {
        return obj.toString();
      } else {
        return obj.toString().substring(0, maxLength) + String.format("###内容过长，只保留%s个字符仅，供参考###", maxLength);
      }
    }
    return result;
  }
}
